package com.flyco.modules.fenxiao.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.fenxiao.entity.WxCpDepartment;
import com.flyco.modules.fenxiao.service.IWxCpDepartmentService;

import java.util.Date;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.system.entity.SysPartner;
import com.flyco.modules.system.service.ISysPartnerService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 部门表
 * @Author: flyco
 * @Date: 2020-07-15
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "部门表")
@RestController
@RequestMapping("/f/fenxiao/wxCpDepartment")
public class WxCpDepartmentController extends BaseController<WxCpDepartment, IWxCpDepartmentService> {
    @Autowired
    private IWxCpDepartmentService wxCpDepartmentService;
    @Autowired
    private ISysPartnerService sysPartnerService;

    /**
     * 分页列表查询
     *
     * @param wxCpDepartment
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "部门表-分页列表查询")
    @ApiOperation(value = "部门表-分页列表查询", notes = "部门表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(WxCpDepartment wxCpDepartment,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<WxCpDepartment> queryWrapper = QueryGenerator.initQueryWrapper(wxCpDepartment, req.getParameterMap());
        Page<WxCpDepartment> page = new Page<WxCpDepartment>(pageNo, pageSize);
        IPage<WxCpDepartment> pageList = wxCpDepartmentService.page(page, queryWrapper);
        if (pageList.getTotal() > 0) {
            List<SysPartner> partners = sysPartnerService.list();
            Map<Long, String> partnerMaps = partners.stream().collect(Collectors.toMap(SysPartner::getId, SysPartner::getName, (key1, key2) -> key2));
            for (WxCpDepartment department : pageList.getRecords()) {
                if (oConvertUtils.isNotEmpty(department.getPartnerId())) {
                    department.setPartnerId_dictText(partnerMaps.get(department.getPartnerId()));
                }
            }
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param wxCpDepartment
     * @return
     */
    @AutoLog(value = "部门表-添加")
    @ApiOperation(value = "部门表-添加", notes = "部门表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WxCpDepartment wxCpDepartment) {
        wxCpDepartmentService.save(wxCpDepartment);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wxCpDepartment
     * @return
     */
    @AutoLog(value = "部门表-编辑")
    @ApiOperation(value = "部门表-编辑", notes = "部门表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WxCpDepartment wxCpDepartment) {
        wxCpDepartmentService.updateById(wxCpDepartment);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "部门表-通过id删除")
    @ApiOperation(value = "部门表-通过id删除", notes = "部门表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        wxCpDepartmentService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "部门表-批量删除")
    @ApiOperation(value = "部门表-批量删除", notes = "部门表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wxCpDepartmentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "部门表-通过id查询")
    @ApiOperation(value = "部门表-通过id查询", notes = "部门表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        WxCpDepartment wxCpDepartment = wxCpDepartmentService.getById(id);
        return Result.ok(wxCpDepartment);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wxCpDepartment
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WxCpDepartment wxCpDepartment) {
        return super.exportXls(request, wxCpDepartment, WxCpDepartment.class, "部门表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WxCpDepartment.class);
    }

}
